package com.yike.user.service.impl;

import com.yike.common.exception.BusinessException;
import com.yike.common.tool.LoginUtil;
import com.yike.common.tool.StringUtil;
import com.yike.user.entity.Agent;
import com.yike.user.entity.AgentAreaSetting;
import com.yike.user.mapper.AgentAreaSettingMapper;
import com.yike.user.mapper.AgentMapper;
import com.yike.user.param.AgentAreaParam;
import com.yike.user.param.AgentAreaSettingParam;
import com.yike.user.service.AgentAreaSettingService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yike.common.base.BaseServiceImpl;
import com.yike.user.utils.AreaTreeBuilder;
import com.yike.user.vo.AgentAreaInfoVo;
import com.yike.user.vo.AgentAreaVo;
import com.yike.user.vo.AreaInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 代理商区域配置表 服务实现类
 *
 * @author Sakura
 * @since 2024-09-13
 */
@Slf4j
@Service
public class AgentAreaSettingServiceImpl extends BaseServiceImpl<AgentAreaSettingMapper, AgentAreaSetting> implements AgentAreaSettingService {

    @Autowired
    private AgentAreaSettingMapper agentAreaSettingMapper;
    @Autowired
    private AgentMapper agentMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveAgentAreaSetting(AgentAreaSettingParam agentAreaSettingParam) throws Exception {
        Agent agent = agentMapper.selectById(agentAreaSettingParam.getAgentId());
        if (agent == null) {
            throw new BusinessException("代理商信息异常");
        }
        // 先将之前的区域设置为不可用，逻辑删除即可
        LambdaQueryWrapper<AgentAreaSetting> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AgentAreaSetting::getAgentId, agentAreaSettingParam.getAgentId());
        agentAreaSettingMapper.delete(wrapper);
        // 保存数据
        for (AgentAreaParam agentAreaParam : agentAreaSettingParam.getAgentAreaParams()) {
            AgentAreaSetting agentAreaSetting = new AgentAreaSetting();
            agentAreaSetting.setAgentId(agentAreaSettingParam.getAgentId());
            BeanUtils.copyProperties(agentAreaParam, agentAreaSetting);
            agentAreaSettingMapper.insert(agentAreaSetting);
        }
        return true;
    }

    @Override
    public List<AgentAreaVo> getAgentAreaSetting(Long agentId) {
        return agentAreaSettingMapper.getAgentAreaSetting(agentId);
    }

    @Override
    public List<AgentAreaInfoVo> getAgentAreas() {
        // 获取当前登录用户区域配置信息
        List<AgentAreaVo> agentAreaVos = agentAreaSettingMapper.getAgentAreaSetting(LoginUtil.getAgentId());
        // 获取下面所有的区域
        Set<AreaInfoVo> areaInfoVos = new HashSet<>();
        for (AgentAreaVo agentAreaVo : agentAreaVos) {
            // 获取省级信息
            AreaInfoVo provInfoVo = agentAreaSettingMapper.getAgentArea(agentAreaVo.getProvCode());
            areaInfoVos.add(provInfoVo);
            // 如果市级code为空则为省级代理，可查看省下面所有的市和区
            if (StringUtil.isBlank(agentAreaVo.getCityCode())) {
                Set<AreaInfoVo> citys = agentAreaSettingMapper.getChildrenAreasByCode(agentAreaVo.getProvCode());
                areaInfoVos.addAll(citys);
            } else {
                // 获取市级信息
                AreaInfoVo citysInfoVo = agentAreaSettingMapper.getAgentArea(agentAreaVo.getCityCode());
                areaInfoVos.add(citysInfoVo);
            }
            // 如果区级code为空则为市级代理，可查看市下面所有的区
            if (StringUtil.isNotBlank(agentAreaVo.getCityCode()) && StringUtil.isBlank(agentAreaVo.getDistCode())) {
                Set<AreaInfoVo> dists = agentAreaSettingMapper.getChildrenAreasByCode(agentAreaVo.getCityCode());
                areaInfoVos.addAll(dists);
            } else {
                if (StringUtil.isNotBlank(agentAreaVo.getDistCode())) {
                    // 获取区级信息
                    AreaInfoVo distInfoVo = agentAreaSettingMapper.getAgentArea(agentAreaVo.getDistCode());
                    areaInfoVos.add(distInfoVo);
                }
            }
        }
        //log.info(JSON.toJSONString(areaInfoVos));
        // 通过递归将数据转换一下
        AreaTreeBuilder builder = new AreaTreeBuilder();
        List<AgentAreaInfoVo> areaTree = builder.buildAreaTree(areaInfoVos);

        //log.info(JSON.toJSONString(areaTree));

        return areaTree;
    }

}
